<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
		"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>

	<title>_Widget.destroy() unit test</title>

	<script type="text/javascript" src="../../dojo/dojo.js" djConfig="isDebug: true"></script>
	<script type="text/javascript">
		require([
			"doh/runner",
			"dojo/_base/declare", "dojo/dom",
			"dijit/registry", "dijit/_WidgetBase", "dijit/_Widget",
			"dojo/domReady!"
		], function(doh, declare, dom, registry, _WidgetBase, _Widget){

			var obj = {
				foo: function(){
					// summary: empty function that we connect to
				}
			};
	
			// Number of times foo was called while TestWidget existed
			var calls = 0;
	
			declare("TestWidget", _Widget, {
				postCreate: function(){
					// Rather odd call to this.connect() For testing the connections are dropped on destroy()
					this.connect(obj, "foo", function(){
						calls++;
					});
				}
			});
	
			var w;

			doh.register("create and destroy", [
				{
					name: "create",
					runTest: function(t){
						w = new TestWidget({id: "w1"}, "w1");

						doh.t(registry.byId("w1"), "widget in registry");

						// since there's no template, the widget just points to the srcNodeRef
						doh.is(w.domNode, dom.byId("w1"), "srcNodeRef read in");

						// test the connection
						doh.is(0, calls, "foo() not called yet");
						obj.foo();
						doh.is(1, calls, "foo() called");
					}
				},
				{
					name: "destroy",
					runTest: function(t){
						w.destroy();

						doh.f(registry.byId("w1"), "widget no longer in registry");

						// test the connection was destroyed
						calls = 0;
						obj.foo();
						doh.is(0, calls, "connection was deleted");

						// test the DOM node was removed
						doh.f(dom.byId("w1"), "DOM Node removed");
					}
				},
				{
					name: "destroy(true)  (preserving DOM node)",
					runTest: function(t){
						w = new TestWidget({id: "w2"}, "w2");

						doh.t(registry.byId("w2"), "widget in registry");
						w.destroy(true);

						doh.f(registry.byId("w2"), "widget no longer in registry");

						// test the DOM node *wasn't* removed
						doh.t(dom.byId("w2"), "DOM Node left");
					}
				},
				{
					name: "create with undefined id",
					runTest: function(t){
						// If id is "specified" as undefined, generate a new one
						w = new TestWidget({id: undefined});

						doh.isNot(undefined, w.id)
					}
				}
			]);

			doh.register("setter calls on creation", function(){
				// Make sure setters are called even for anonymous classes (#12122),
				// and even when there's no value explicitly specified in the parameters
				var fooSetterCalled,
					Widget = declare([_Widget], {
						foo: 345,
						_setFooAttr: function(val){
							fooSetterCalled = val;
							this.foo = val;
						}
					});

				new Widget();

				doh.is(345, fooSetterCalled, "fooSetterCalled");
			});

			doh.register("tweaking params in postMixInProperties", function(){
				// Tests that property changes in postMixInProperties() are not lost (#16080)
				var Test = declare(_WidgetBase, { foo: "bar", postMixInProperties: function(){ this.foo = "baz"; } });
				var t = new Test({ foo: "blah" });
				doh.is("baz", t.foo);
			});

			doh.run();
		});

	</script>
</head>
<body class="claro">

	<div id="w1"></div>
	<div id="w2"></div>
</body>
</html>
